"""
    with语句：
            又称上下文管理器，在处理文件时，无论是否产生异常，都能保证 with 语句执行完毕后
            关闭已经打开的文件，这个过程中是自动的，无需手动操作。
    语法结构：
            with open(...) as file:
                pass

"""
def write_fun():
    with open('aa.txt', 'w', encoding='utf-8') as file:
        # 写入
        file.write('2008北京欢迎您')


def read_fun():
    with open('aa.txt', 'r', encoding='utf-8') as file:
        # 读取
        print(file.read())


# 文件复制
def copy(src_file, target_file):
    with open(src_file, 'r', encoding='utf-8') as file:
        with open(target_file, 'w', encoding='utf-8') as file2:
            file2.write(file.read())    # 读取的内容直接写进文件


if __name__ == '__main__':
    write_fun()
    read_fun()
    copy('aa.txt', 'bb.txt')

